#include <assert.h>
#include <windows.h>

#define _GVLOCK_

/*--
     A mutual exclusive lock basically working as a wrapper for Critical section
--*/
class CLock
{
private:
    CRITICAL_SECTION m_Crist;
    DWORD m_OwnerThread;
    BOOL m_Initialized;
    DWORD m_Error;
    CLock& operator=(const CLock& lock); //No assignment

    
public:
    CLock() :
      m_OwnerThread(0),
      m_Initialized(FALSE),
      m_Error(0)
          
    {
        if (InitializeCriticalSectionEx(&m_Crist,1000,0))
        {
            m_Initialized=TRUE;
        }
        else
        {
            m_Error=::GetLastError();
        }
    }

    ~CLock()
    {
        DeleteCriticalSection(&m_Crist);
    }
 

    void Enter()
    {
        assert(m_OwnerThread==0);
        
        EnterCriticalSection(&m_Crist);
        this->m_OwnerThread=::GetCurrentThreadId();
    }

    void Leave()
    {
        assert(m_OwnerThread!=0);

        LeaveCriticalSection(&m_Crist);
        this->m_OwnerThread=0;
    }

    DWORD OwnerThread()
    {
        return this->m_OwnerThread;
    }

    DWORD Error()
    {
        return m_Error;
    }
    
};